﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI.WebControls;

namespace NotAClue.Web.DynamicData
{
    public class ParentChangeNotifyingFieldTemplate : FieldTemplateUserControl, IChangeNotifyingFieldTemplate
    {
        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The state.</value>
        public virtual ListItem State { get; private set; }

        /// <summary>
        /// publish event.
        /// </summary>
        public event ChangingAwareEventHandler StateChanged;

        /// <summary>
        /// Gets a value indicating whether this instance has events.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has events; otherwise, <c>false</c>.
        /// </value>
        public new Boolean HasEvents 
        {
            get { return StateChanged != null; }
        }

        /// <summary>
        /// Sets the state.
        /// </summary>
        /// <param name="listItem">The list item.</param>
        /// <remarks></remarks>
        public void SetState(ListItem listItem)
        {
            State = listItem;
        }

        /// <summary>
        /// Raises the event checking first that an event if hooked up
        /// </summary>
        /// <param name="listItem">The value of the currently selected item</param>
        public void RaiseStatusChanged(ListItem listItem)
        {
            // make sure we have a handler attached
            if (StateChanged != null)
            {
                //raise event
                StateChanged(this, new ChangingAwareEventArgs(listItem));
            }
        }
    }
}